//
//  GRXPlayView.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 16/1/1.
//  Copyright © 2016年 grx. All rights reserved.
//

#import "GRXPlayView.h"
#import "GRXImageCell.h"
@interface GRXPlayView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) UIPageControl *pageControl;

/**图片数组*/
@property (nonatomic,strong)NSArray *images;

/**轮播器*/
@property (nonatomic, weak) UICollectionView *collectionView;

/**定时器  */
@property (weak, nonatomic) NSTimer *timer;


@end

@implementation GRXPlayView

static NSString * const playCell = @"playCell";


- (NSArray *)images{
    if (!_images) {
        _images = [NSArray array];
        _images = @[@"image-1",@"image-2",@"image-3",@"image-4",@"image-5"];
    }
    return _images;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //添加轮播图
        [self setUpcollectionView];
        
        
        //添加页码显示
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        pageControl.numberOfPages = self.images.count;
        [self addSubview:pageControl];
        self.pageControl = pageControl;
    }
    return self;
}

- (void)setUpcollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(kScreenW, 190);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.bounces = NO;
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    self.collectionView = collectionView;
    collectionView.showsHorizontalScrollIndicator = NO;
    
    collectionView.pagingEnabled = YES;
    
    
    [self insertSubview:collectionView atIndex:1];
    
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:2500  inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

    //注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"GRXImageCell" bundle:nil] forCellWithReuseIdentifier:playCell];
    
    //开启定时器
    [self startTimer];
    

}

 -(void)nextPage
{
       //1）获取当前正在展示的位置
        NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
    
//    　 2）计算出下一个需要展示的位置
        NSInteger nextItem = currentIndexPath.item + 1;
        NSInteger nextSection = currentIndexPath.section;
        NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    
        if (nextItem == 5000) {
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:2500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
            return;
        }
    
        if (nextItem == 0) {
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:2500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
            return;
        }
        
        [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
    
        self.pageControl.currentPage = nextItem % 5;
 }

#pragma mark ---布局子控件
- (void)layoutSubviews{
    UICollectionView *collectionView = self.subviews.firstObject;
    collectionView.frame = CGRectMake(0, 0, self.width, self.height);
    
    self.pageControl.frame = CGRectMake(self.width * 0.5 - 30, self.height-20, 60, 20);
}


#pragma mark ---collectionView数据源

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5000;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GRXImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:playCell forIndexPath:indexPath];
    
    cell.imageView.image = [UIImage imageNamed:self.images[indexPath.item % 5]];
    
    cell.titleLabel.hidden = YES;
    
    return cell;
}

#pragma mark ---私有方法

/**
 *设置定时器
 */
- (void)startTimer{
    //添加定时器
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
    //添加到主运行循环中
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    self.timer = timer;
}

- (void)stopTimer{
    
    [self.timer invalidate];
    self.timer = nil;
}



#pragma mark ---collectionView代理方法
//拖动结束的时候
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    //拖动结束的时候,让pagecontrol调到相应位置
    self.pageControl.currentPage = (int)(scrollView.contentOffset.x  / self.width) % 5;
    
    //添加定时器
    [self startTimer];
}


//刚开始拖动的时候
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self stopTimer];
}



//轮播图的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%zd",indexPath.item % 5);
}

@end
